import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import javax.swing.JFileChooser;



public class Problema {

	BufferedReader bf;
	FileReader fr;
	File f;
	File[] lista;
	File fb;
	String nome;
	ArrayList<Cidade> cds = new ArrayList<Cidade>();
	ArrayList<Problema> pbs = new ArrayList<Problema>();
	double[][] dists;
	
	public ArrayList<Cidade> getCds() {
		return cds;
	}

	public void setCds(ArrayList<Cidade> cds) {
		this.cds = cds;
	}

	public ArrayList<Problema> lerTSP(){
		
		
		
		f = new File("/home/david/Desktop/TCC For Real/TSP/");
		lista = f.listFiles();
		
	//	fb = new File("/home/david/Desktop/TCC For Real/TSP/berlin52.tsp");
		
		int i = 1;
		
		try{
			for (int j = 0; j < lista.length/3  ; j++) {
				fr = new FileReader(lista[j]);
				bf = new BufferedReader(fr);
				
				String x =  bf.readLine().trim();
				nome = x.substring(6, x.length());
				while(x != null){
					if(x.startsWith(String.valueOf(i))){
						
						//System.out.println(Integer.parseInt(x.split("\\s+")[0]) + " " + Double.parseDouble(x.split("\\s+")[1]) +" " +Double.parseDouble(x.split("\\s+")[2]));
						
						cds.add(new Cidade(Integer.parseInt(x.split("\\s+")[0])-1,
								Double.parseDouble(x.split("\\s+")[1]),
								Double.parseDouble(x.split("\\s+")[2])));
						i++;
					}
					x = bf.readLine();
					if(x != null)
						x = x.trim();
				}
				pbs.add(new Problema(nome, (ArrayList<Cidade>)cds.clone()));
				cds.clear();
				System.gc();
				i = 1;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return pbs;
	} 
	
	

	
	public Problema(String nome, ArrayList<Cidade> cds) {
		super();
		this.nome = nome;
		this.cds = cds;
	}
	public Problema() {}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public void listaCidades(){
		
		for(Cidade c:cds){
			System.out.println(c.getNum() + " "+ c.getPosX() + " " + c.getPosY());
		}
		
	}
	
	public double[][] calcularDistancias(){
		 dists = new double[cds.size()][cds.size()];
		
		   for(int i = 0; i < dists.length; i++)
	            for(int j = 0; j < dists.length; j++)
	            {
	            	dists[i][j] = Math.sqrt(Math.pow(cds.get(j).getPosX() - cds.get(i).getPosX(), 2) + 
	            			Math.pow(cds.get(j).getPosY() - cds.get(i).getPosY(), 2));
	            }
		   return dists;
	}
	
	
}
